import { registerMicroApps, start } from 'qiankun';
import actions from './qiankunActions';
const env = import.meta.env;

const subApps = [
  {
    name: 'hr', // 子应用名称，跟主应用注册的微应用名称package.json一致
    entry: env.VITE_MICRO_APPROVAL_URL, // 子应用入口，本地环境下指定端口
    container: '#sub-container', // 挂载子应用的dom
    activeRule: '/app/approval', // 路由匹配规则
    props: {
      appCode: 'appcode',
      actions
    } // 主应用与子应用通信传值
  }
  // {
  //   name: "app-vue2",
  //   entry: "//localhost:7002",
  //   container: "#sub-container",
  //   activeRule: "/app/app-vue2",
  //   props: {},
  // },
];

class QiankunConfig {
  constructor() {
    try {
      registerMicroApps(subApps, {
        beforeLoad: [
          (app) => {
            // console.log('before load', app);
          }
        ],
        beforeMount: [
          (app) => {
            // console.log('before mount', app);
          }
        ],
        afterUnmount: [
          (app) => {
            // console.log('父应用卸载', app);
          }
        ],
        afterMount: [
          (app) => {
            // console.log('父应用加载');
            // console.log('before afterMount', app);
            // QiankunActions.SetSonStorage("token", "token");
          }
        ]
      });
    } catch (err) {
      console.log(err);
    }
  }
  loadApp = () => {
    if (!window.qiankunStarted) {
      window.qiankunStarted = true;
      start({
        sandbox: {
          experimentalStyleIsolation: true // 微应用间样式隔离
          // strictStyleIsolation: true
        },
        prefetch: false //预加载
      });
    }
  };
}
const qiankun = new QiankunConfig();
export default qiankun;
